package com.juyu.miliao.api.service;

import com.juyu.miliao.api.constant.UserTypeEnum;
import com.juyu.miliao.api.dao.mysql.QuestionAndAnswerInfoDao;
import com.juyu.miliao.api.dao.redis.QuestionAndAnswerInfoRedisDao;
import com.juyu.miliao.api.dao.redis.QuestionAndAnswerPayInfoRedisDao;
import com.juyu.miliao.api.dao.redis.usercache.UserCacheVo;
import com.juyu.miliao.api.dao.redis.usercache.UserInfoCache;
import com.juyu.miliao.api.domain.QuestionAndAnswerInfo;
import com.juyu.miliao.api.domain.vo.QuestionAndAnswerVo;
import com.juyu.miliao.api.util.PageUtil;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 偷听service
 *
 * @author xuanqicheng
 * @create 2018-18-10:18 AM
 */
@Service
public class EavesdroppingService {

    @Autowired
    private QuestionAndAnswerInfoRedisDao questionAndAnswerInfoRedisDao;

    @Autowired
    private QuestionAndAnswerPayInfoRedisDao questionAndAnswerPayInfoRedisDao;

    @Autowired
    private UserInfoCache userInfoCache;

    /**
     * 获取VIP秘密小屋列表
     *
     * @param page
     * @param pageSize
     */
    public List<QuestionAndAnswerVo> getVipVoiceList(Integer page, Integer pageSize) {

        int startNum = PageUtil.getOffset(page, pageSize);

        Map<String, Object> parameterMap = new HashMap<>(0);

        parameterMap.put("startNum", startNum);

        parameterMap.put("pageSize", pageSize);

        List<QuestionAndAnswerInfo> list = questionAndAnswerInfoRedisDao.selectVipVoiceList(parameterMap);

        List<QuestionAndAnswerVo> questionAndAnswerVoList = new ArrayList<>(0);
        for (QuestionAndAnswerInfo obj : list) {

            QuestionAndAnswerVo entity = getQuestionAndAnswerInfoToQuestionAndAnswerVo(obj);

            questionAndAnswerVoList.add(entity);

        }

        return questionAndAnswerVoList;
    }


    /**
     * 根据Id获取对应语音
     *
     * @param userId     偷听用户Id
     * @param questionId
     */
    public String getVipVoice(long userId, Long questionId) {
        String url = "";
        QuestionAndAnswerInfo obj = questionAndAnswerInfoRedisDao.getVoiceByQuestionId(questionId);

        if (obj != null) {
            if (userId != obj.getAnswerUserId()) {
                questionAndAnswerInfoRedisDao.setAnswerEavesdroppingNumber(questionId, userId);
            }
            url = obj.getAnswerUrl();
        }

        return url;
    }


    public List<QuestionAndAnswerVo> getSecretVoiceList(Long userId, Integer page, Integer pageSize) {
        int startNum = PageUtil.getOffset(page, pageSize);

        Map<String, Object> parameterMap = new HashMap<>(0);

        parameterMap.put("pageSize", pageSize);

        parameterMap.put("startNum", startNum);

        parameterMap.put("userId", userId);


        UserCacheVo userCacheVo=userInfoCache.getUserCacheInfo(userId);

        parameterMap.put("type",userCacheVo.getIsHost());

        List<QuestionAndAnswerInfo> list = null;
        /**
         * type 为1是查询主播回答的问题，2：查询用户提问的问题
         */
        if (userCacheVo.getIsHost() == 1) {
            list = questionAndAnswerInfoRedisDao.selectSecretVoiceByAnchorList(parameterMap);
        } else {

            list = questionAndAnswerInfoRedisDao.selecrSecretVoiceByNotAnchorList(parameterMap);
        }

        List<QuestionAndAnswerVo> questionAndAnswerVoList = new ArrayList<>(0);

        for (QuestionAndAnswerInfo obj : list) {

            QuestionAndAnswerVo entity = getQuestionAndAnswerInfoToQuestionAndAnswerVo(obj);

            questionAndAnswerVoList.add(entity);

        }

        return questionAndAnswerVoList;

    }


    /**
     * 将QuestionAndAnswerInfo 内容装换成QuestionAndAnswerVo
     *
     * @param obj
     * @return
     */
    private QuestionAndAnswerVo getQuestionAndAnswerInfoToQuestionAndAnswerVo(QuestionAndAnswerInfo obj) {
        QuestionAndAnswerVo entity = new QuestionAndAnswerVo();

        BeanUtils.copyProperties(obj, entity);

        UserCacheVo questionUser = userInfoCache.getUserCacheInfo(obj.getQuestionUserId());

        if (questionUser != null) {
            entity.setQuestionUserNickName(questionUser.getNickName());


            entity.setQuestionUserIcon(questionUser.getIcon());

            entity.setQuestionUserSex(questionUser.getSex());

            entity.setQuestionUserAge(questionUser.getAge());

            entity.setQuestionMGrade(questionUser.getMGrade());

            entity.setQuestionVGrade(questionUser.getVGrade());

            entity.setQuestionIsVip(questionUser.getIsVip());
            entity.setQuestionIsHost(questionUser.getIsHost());
            entity.setQuestionVipLevel(questionUser.getVipLevel());
            entity.setQuestionStar(questionUser.getStar());
        }


        UserCacheVo answerUser = userInfoCache.getUserCacheInfo(obj.getAnswerUserId());
        if (answerUser != null) {
            entity.setAnswerUserNickName(answerUser.getNickName());

            entity.setAnswerUserIcon(answerUser.getIcon());

            entity.setAnswerUserAge(answerUser.getAge());

            entity.setAnswerUserSex(answerUser.getSex());

            entity.setAnswerVGrade(answerUser.getVGrade());

            entity.setAnswerMGrade(answerUser.getMGrade());

            entity.setAnswerIsVip(answerUser.getIsVip());

            entity.setAnswerIsHost(answerUser.getIsHost());

            entity.setAnswerVipLevel(answerUser.getVipLevel());

            entity.setAnswerStar(answerUser.getStar());

            entity.setAnswerUrl("");
        }


        long number = questionAndAnswerInfoRedisDao.getAnswerEavesdroppingNumber(obj.getId());

        entity.setEavesdroppingNumber(number);

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd HH:mm");

        String localTime = dateTimeFormatter.format(obj.getCreateTime());

        entity.setShowTime(localTime);
        return entity;
    }

    /**
     * 列表偷听
     *
     * @param userId
     * @param questionId
     * @return
     */
    public String getLittleSecretUrl(long userId, Long questionId) {
        QuestionAndAnswerInfo obj = questionAndAnswerInfoRedisDao.getVoiceByQuestionId(questionId);
        UserCacheVo user = userInfoCache.getUserCacheInfo(userId);
        //判断用户是不是vip,用户是vip直接给地址，如果不是执行如下逻辑
        if (user.getIsVip() == 2 && user.getIsSvip()==2 && user.getIsPlatinumVip()==2) {
            //判断用户是不是提问者或回答者如果两个条件都满足给地址，如果两个条件都不满足，执行如下逻辑
            if (obj.getAnswerUserId() != userId && obj.getQuestionUserId() != userId) {
                //查询用户是否购买过此条语音,如果没有购买过执行如下逻辑
                boolean bl = questionAndAnswerPayInfoRedisDao.checkUserIsBy(questionId, userId);
                if (!bl) {
                    throw new ConsumeRuntimeException("用户没有购买过，请支付5金币或充值VIP");
                }else{
                    if (obj.getAnswerUserId() != userId && obj.getQuestionUserId()!=null) {
                        questionAndAnswerInfoRedisDao.setAnswerEavesdroppingNumber(questionId, userId);
                    }
                    return obj.getAnswerUrl();
                }
            } else {

                if (obj.getAnswerUserId() != userId && obj.getQuestionUserId()!=null) {
                    questionAndAnswerInfoRedisDao.setAnswerEavesdroppingNumber(questionId, userId);
                }

                return obj.getAnswerUrl();
            }
        } else {
            if (obj.getAnswerUserId() != userId && obj.getQuestionUserId()!=null) {
                questionAndAnswerInfoRedisDao.setAnswerEavesdroppingNumber(questionId, userId);
            }
            return obj.getAnswerUrl();
        }
    }

    /**
     * 根据状态查看列表
     *
     * @param userId
     * @param type     1: 待回答，2：以回答，3：以提问
     * @param page
     * @param pageSize
     * @return
     */
    public List<QuestionAndAnswerVo> selectListByAnchor(Integer type, long userId, Integer page, Integer pageSize) {
        int startNum = PageUtil.getOffset(page, pageSize);
        Map<String, Object> parameterMap = new HashMap<>(0);
        parameterMap.put("type", type);
        parameterMap.put("userId", userId);
        parameterMap.put("startNum", startNum);
        parameterMap.put("pageSize", pageSize);

        int isHost = 2;

        UserCacheVo userCacheVo = userInfoCache.getUserCacheInfo(userId);

        if (userCacheVo != null) {
            isHost = userCacheVo.getIsHost();
        }


        List<QuestionAndAnswerInfo> list = new ArrayList<>();

        if (type == 3) {
            list = questionAndAnswerInfoRedisDao.selectListByAll(parameterMap);
        } else {
            if (isHost == 1) {
                if (type == 1 || type == 2) {
                    list = questionAndAnswerInfoRedisDao.selectListByAnchor(parameterMap);
                }
            }

        }

        List<QuestionAndAnswerVo> questionAndAnswerVoList = new ArrayList<>(0);

        for (QuestionAndAnswerInfo obj : list) {

            QuestionAndAnswerVo entity = getQuestionAndAnswerInfoToQuestionAndAnswerVo(obj);

            questionAndAnswerVoList.add(entity);

        }

        return questionAndAnswerVoList;
    }

    public List<QuestionAndAnswerVo> selectListByAll(long userId, Integer page, Integer pageSize) {
        int startNum = PageUtil.getOffset(page, pageSize);
        Map<String, Object> parameterMap = new HashMap<>(0);
        parameterMap.put("userId", userId);
        parameterMap.put("startNum", startNum);
        parameterMap.put("pageSize", pageSize);

        List<QuestionAndAnswerInfo> list = questionAndAnswerInfoRedisDao.selectListByAll(parameterMap);

        List<QuestionAndAnswerVo> questionAndAnswerVoList = new ArrayList<>(0);

        for (QuestionAndAnswerInfo obj : list) {

            QuestionAndAnswerVo entity = getQuestionAndAnswerInfoToQuestionAndAnswerVo(obj);

            questionAndAnswerVoList.add(entity);

        }

        return questionAndAnswerVoList;
    }
}
